﻿using System.Collections.Generic;
using System.Linq;

namespace uTILLIty.ADExtractor.Core.Formatters
{
	public class FormatterFactory
	{
		private readonly List<IFormatter> _formatters = new List<IFormatter>(10);
		public void RegisterFormatter(IFormatter instance)
		{
			if (_formatters.Any(f => f.GetType() == instance.GetType()))
				return;
			_formatters.Add(instance);
		}

		public object TryFormat(string attributeName, object value, string valueSeparator)
		{
			if (value == null)
				return string.Empty;

			var formatter = _formatters.FirstOrDefault(f => f.CanFormat(attributeName));
			if (formatter == null) 
			return value;
			
			value = formatter.Format(attributeName, value, valueSeparator);
			return value;
		}
	}
}